package com.cc2019.buke.home.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.cc2019.buke.R;


public class AddAndReduce extends LinearLayout {

    private final TextView no_text;

    public AddAndReduce(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.add_sum_view,this);

        ImageView buycar_reduce = view.findViewById(R.id.buycar_reduce);
        ImageView buycar_add = view.findViewById(R.id.buycar_add);
        no_text = view.findViewById(R.id.buycar_no);
        buycar_reduce.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int no = Integer.parseInt(no_text.getText().toString());
                if (no>2){
                    no--;
                    no_text.setText(no+"");
                }else {
                    Toast.makeText(context,"不能在减少了",Toast.LENGTH_SHORT).show();
                }
            }
        });
        buycar_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int no = Integer.parseInt(no_text.getText().toString());
                if (no<10){
                    no++;
                    no_text.setText(no+"");
                }else {
                    Toast.makeText(context,"无库存，不能在增加",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public int getTotal(){
        return  Integer.parseInt(no_text.getText().toString());
    }
}
